package br.com.pnfp.web.control.actions.relatorios;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import br.com.pnfp.web.control.actions.DefaultAction;
import br.com.pnfp.web.control.forms.relatorios.QtdeNotaFiscalForm;
import br.com.pnfp.web.model.vo.Usuario;
import br.com.pnfp.web.model.vo.report.QtdeCupomUsuario;

public class QtdeNotaFiscalAction extends DefaultAction {
	
	private static final Logger logger = Logger.getLogger(QtdeNotaFiscalAction.class); 
	
	public ActionForward iniciar(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		QtdeNotaFiscalForm qtdeNotaFiscalForm = (QtdeNotaFiscalForm) form;
		qtdeNotaFiscalForm.setTipoData(1); //Data de Cadastro
		
		QtdeCupomUsuario vo = new QtdeCupomUsuario();
		vo.setUsuarioSistema(getUsuario(request));
		vo.setDataDe(new Date());
		vo.setDataAte(new Date());
		vo.setTipoConsulta(qtdeNotaFiscalForm.getTipoData());
		
		getCupomDAO().consultaQtdeCupom(vo);
		request.setAttribute("listaResultado", vo);
		
		request.setAttribute("dataDe", getDateFormatDojo().format(new Date()));
		request.setAttribute("dataAte", getDateFormatDojo().format(new Date()));
		
		return mapping.findForward("body");
	}
	
	public ActionForward consultar(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		QtdeNotaFiscalForm qtdeNotaFiscalForm = (QtdeNotaFiscalForm) form;
		
		QtdeCupomUsuario vo = new QtdeCupomUsuario();
		vo.setDataDe(getDateFormatDojo().parse(qtdeNotaFiscalForm.getDataDe()));
		vo.setDataAte(getDateFormatDojo().parse(qtdeNotaFiscalForm.getDataAte()));
		vo.setTipoConsulta(qtdeNotaFiscalForm.getTipoData());
		
		if(qtdeNotaFiscalForm.getUsuarioConsultar() != null && qtdeNotaFiscalForm.getUsuarioConsultar() > 0) {
			Usuario consulta = new Usuario();
			consulta.setCodigo(qtdeNotaFiscalForm.getUsuarioConsultar());
			consulta.setUsuarioSistema(getUsuario(request));
			
			Usuario usuario = getUsuarioDAO().consultar(consulta).get(0);
			vo.setUsuarioSistema(usuario);
		} else {
			vo.setUsuarioSistema(getUsuario(request));
		}
		
		getCupomDAO().consultaQtdeCupom(vo);
		request.setAttribute("listaResultado", vo);	
		
		request.setAttribute("dataDe", getDateFormatDojo().format(vo.getDataDe()));
		request.setAttribute("dataAte", getDateFormatDojo().format(vo.getDataAte()));
		
		return mapping.findForward("body");
	}
}
